package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class In_enNewsAnalyse implements AnalyseNews {

	//http://oil.in-en.com/html/oil-08420842231818261.html
	private static final String[] regex={
		"http://oil.in-en.com/html/oil-[0-9]*.shtml",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("h1#title").text();
		date=DateUtils.matchDate(doc.select("div.c_copy").text());
		content =HtmlCleaner.getContentHtml(url, doc.select("div.content"));
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(title);
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);
//		if(news.getContent().equals("评论内容只代表网友观点，与本站立场无关！ 通行证：用户名 密码")){
//			news.setContent("");
//		}
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://oil.in-en.com/html/oil-2571461.shtml";
		In_enNewsAnalyse test=new In_enNewsAnalyse();
		System.out.println(url);
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		//System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
